package com.example.idachuappone;

import java.io.File;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.example.idachuappone.index.dao.impl.WelcomeDaoImpl;
import com.example.idachuappone.index.dto.WelcomeDto;
import com.example.idachuappone.index.welcomentity.InitData;
import com.example.idachuappone.index.welcomentity.Welcome;
import com.example.idachuappone.person.entity.Order;
import com.example.idachuappone.person.method.OrderMethod;
import com.example.idachuappone.person.method.OrderMethod.SetOrderList;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class LoadingActivity extends BaseActivity {
	public static Welcome welcome;
	private BitmapUtils bu;
	private ImageView img_yujiazai;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loading);
		getWindow().setFlags(AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT,
				AccessibilityNodeInfoCompat.ACTION_NEXT_HTML_ELEMENT);
		welcome = new Welcome();
		this.img_yujiazai = (ImageView) findViewById(R.id.img_yujiazai);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		int version = getVersion();
		if (PrefUtil.getInstance(this).getVersion() < version) {
			try {
				PrefUtil.getInstance(this).setIsAddressIndexDialog(true);
				Intent intent = new Intent(this, FristActivity.class);
				intent.putExtra("version", version);
				startActivity(intent);
			} catch (Exception e) {
			} finally {
				finish();
			}
			return;
		}
		loadingShanPing();
		loading();
	}

	private void loadingShanPing() {
		new Thread() {
			@Override
			public void run() {

				try {
					Thread.sleep(1000);

				} catch (Exception e) {
				} finally {
					LoadingActivity.this.initWelcomeData();
				}
			}
		}.start();
	}

	private void loading() {
		new Thread() {
			@Override
			public void run() {

				try {
					Thread.sleep(3000);

				} catch (Exception e) {
				} finally {
					LoadingActivity.this.isLogin();
					tiao();
				}
			}
		}.start();
	}

	private void isLogin() {
		OrderMethod.getOrderList(this, Constant.PROMPTLY_ALL, new SetOrderList() {
			@Override
			public void onSuccess(List<Order> list, String result) {
				try {
					if (new JSONObject(result).getInt("code") == 401) {
						PrefUtil.getInstance(LoadingActivity.this).setUID(null);
						PrefUtil.getInstance(LoadingActivity.this).setAuth(null);
						PrefUtil.getInstance(LoadingActivity.this).setMa(null);
						PrefUtil.getInstance(LoadingActivity.this).setFimallyLocateCoor(null);
						PrefUtil.getInstance(LoadingActivity.this).setArea(null);
						PrefUtil.getInstance(LoadingActivity.this).setXiaoQu(null);
						PrefUtil.getInstance(LoadingActivity.this).setDoor(null);
						PrefUtil.getInstance(LoadingActivity.this).setName(null);
						PrefUtil.getInstance(LoadingActivity.this).setUserAddressId(null);
						PrefUtil.getInstance(LoadingActivity.this).setPhone(null);
						AppShareData.isPersonFrag = true;
						AppShareData.isClearData = true;
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFail() {
			}
		});
	}

	public int getVersion() {
		try {
			return getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	private void tiao() {
		startActivity(new Intent(this, MainTWo.class));
	}

	private void initWelcomeData() {
		NetUtil.get(this, Constant.PACKAGES, 100, new RequestCallBack<String>() {
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				LoadingActivity.this.startActivity(new Intent(LoadingActivity.this, MainTWo.class));
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				LoadingActivity.this.setDataCache(responseInfo.result);
				String json = NetUtil.unescapeUnicode(responseInfo.result);
				try {
					welcome = new WelcomeDto().getWelcome(LoadingActivity.this, json);
					if (welcome.getFlash() != null && welcome.getFlash().length() > 0) {
						File file = LoadingActivity.this.bu.getBitmapFileFromDiskCache(welcome.getFlash());
						if (file != null) {
							Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
							LoadingActivity.this.img_yujiazai.setImageBitmap(bitmap);
							if (bitmap.isRecycled()) {
								bitmap.recycle();
								return;
							}
							return;
						}
						LoadingActivity.this.bu.display(LoadingActivity.this.img_yujiazai, welcome.getFlash());
					}
				} catch (Exception e) {
					MainToast.show(LoadingActivity.this, e.getMessage(), 0);
				}
			}
		});
	}

	private void setDataCache(String data) {
		if (initdataCache() != null) {
			new WelcomeDaoImpl(this).update(new InitData("maindata", data));
		} else {
			new WelcomeDaoImpl(this).add(new InitData("maindata", data));
		}
	}

	private InitData initdataCache() {
		return new WelcomeDaoImpl(this).selectByName("maindata");
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return true;
	}
}
